package com.xyz.app.controller;

import com.free.common.vo.ResultModel;
import com.xyz.app.constants.EnumMesasgeApp;
import com.xyz.app.shiro.UserShiroFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @RequestMapping(value = "/login.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel login(HttpServletRequest request){
        String exception = request.getAttribute(UserShiroFilter.LOGIN_FAIL_EXCEPTION).toString();
        if(exception.equals("com.xyz.app.exception.EmptyAccountException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200009, null);
        }
        if(exception.equals("com.xyz.app.exception.EmptyPasswordException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200010, null);
        }
        if(exception.equals("com.xyz.app.exception.EmptyCapatcharException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200005, null);
        }
        if(exception.equals("com.xyz.app.exception.InvalidCaptachaException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200006, null);
        }
        if(exception.equals("com.xyz.shiro.exception.InvalidAccountException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200011, null);
        }
        if(exception.equals("com.xyz.shiro.exception.InvalidAccntPwdException")){
            return ResultModel.failure(null, EnumMesasgeApp.E0200008, null);
        }
        if(StringUtils.isNotEmpty(exception)){
            return ResultModel.failure();
        }
        return ResultModel.success();
    }

}
